import Vue from 'vue'
import Vuex from 'vuex'
import { Dialog } from 'vant';

Vue.use(Dialog);
Vue.use(Vuex)

const state = {
    cart:{},
    num:1,
    total:0
}
// 存储这种形式 cart:{"1":{count:"1",goods:{...}},"2",{小茗同学}}
const mutations = {
    addShopCart(state,goods){
        Dialog.confirm({
            title: '小提示',
            message: '是否购买'+ goods.name + "？"
          }).then(() => {
            // 如果存储里面有数据就获取
            var getStorage = localStorage.getItem("cart")
            if(getStorage){
                state.cart = JSON.parse(getStorage)
            }
            if(state.cart[goods.id]){
                state.cart[goods.id].count++
                state.cart[goods.id].goods.total = state.cart[goods.id].count * state.cart[goods.id].goods.price
            }else{
                state.cart[goods.id] = {count:1,goods:goods}
            }
            localStorage.setItem("cart",JSON.stringify(state.cart))
            // window.console.log("购买一个"+goods.name+"成功")
            // window.console.log(state.cart)
          }).catch(() => {
            // on cancel
          });
    },
    reduceShopCart(state,goods){
        // 如果存储里面有数据就获取
        var getStorage = localStorage.getItem("cart")
        state.cart = JSON.parse(getStorage)
        for(let i in state.cart) {
            if(i == goods.id){
                state.cart[i].count--
                state.cart[i].goods.total = state.cart[i].count * state.cart[i].price
            }
        }
        localStorage.setItem("cart",JSON.stringify(state.cart))
        // window.console.log("减少一个成功")
        // window.console.log(state.cart)
    },

    add(state,goods){
        var getStorage = localStorage.getItem("cart")
        state.cart = JSON.parse(getStorage)
        for(let i in state.cart) {
            if(i == goods.id){
                state.cart[i].count++
                state.cart[i].goods.total = state.cart[i].count * state.cart[i].goods.price
            }
        }
        localStorage.setItem("cart",JSON.stringify(state.cart))
        // window.console.log(state.cart)
        // window.console.log("成功加一个商品")
    },
    reduce(state,goods){
        var getStorage = localStorage.getItem("cart")
        state.cart = JSON.parse(getStorage)
        for(let i in state.cart) {
            if(i == goods.id){
                if(state.cart[i].count == 1){
                   return state.cart[i].count = 1
                }
                state.cart[i].count--
                state.cart[i].goods.total = state.cart[i].count * state.cart[i].goods.price
            }
        }
        localStorage.setItem("cart",JSON.stringify(state.cart))
        // window.console.log(state.cart)
        // window.console.log("成功加一个商品")
    },
    clear(state){
        // localStorage.clear()
        var getStorage = localStorage.getItem("cart")
        state.cart = JSON.parse(getStorage)
        for(let i in state.cart) {
            if(state.cart[i].goods.isTrue == true){
                // window.console.log(state.cart)
                delete state.cart[i]
                // window.console.log(state.cart[i])
                localStorage.setItem("cart",JSON.stringify(state.cart))
            }
        }
        // state.cart = {}
    }
}

const actions = {
    addShop(context,goods){
        context.commit("addShopCart",goods)
    },
    reduceShop(context,goods){
        context.commit("reduceShopCart",goods)
    },
    addNum(context,goods){
        context.commit("add",goods)
    },
    reduceNum(context,goods){
        context.commit("reduce",goods)
    },
    allClear(context){
        context.commit("clear")
    }
}

const store = new Vuex.Store({
    state,
    mutations,
    actions
})

export default store
